package com.gouaixin.bee.beeframework2.model;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gouaixin.bee.beeframework2.MyAPI;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.GUANZHU;
import com.gouaixin.bee.beeframework2.bean.ServiceMsg;
import com.gouaixin.bee.beeframework2.bean.ZhenxinhuaFriend;
import com.gouaixin.bee.beeframework2.bean.getChatFriendsResponse;
import com.gouaixin.bee.beeframework2.dao.ServiceMsgDao;
import com.gouaixin.bee.beeframework2.dao.zhenxinhuaFriendDao;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.XHttpUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2015/4/23.
 */
//缘分小秘书的消息发送器
public class ServiceChatModel {

    //提醒音效
    private static SoundPool soundPool;
    static int aid;

    //准备音效资源
    private static void initSoudPool(Context context) {
        /***
         *  开始音效（SoudPool实现）
         */
        // 创建SoudPool的对象（最大音乐数目，音乐的类型，音乐的品质）
        soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
//        // 加载音乐
        aid = soundPool.load(context, R.raw.notify02, 1);
    }


    /**
     * 接受缘分小秘书的消息
     * 2.封装消息
     * 4.添加消息到本地
     */
    public static void receiveServiceMsg(final Context context, String msg) {
        initSoudPool(context);

        //2.封装消息

        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

        ServiceMsg bean = new ServiceMsg();

        bean.setMsg(msg);
        bean.setType(ServiceMsg.RECEIVER);
        bean.setTime(time);


        Log.e("msg", "接受缘分小秘书的消息:bean=" + new Gson().toJson(bean));


        //3.添加消息到本地

        ServiceMsgDao.add(context, bean);


        //消息提醒

        mLog.e("接受缘分小秘书的消息:播放音效");

        soundPool.play(aid, 1, 1, 0, 0, 1);


        //5.配置数字相关

        //判断当前的聊天状态

        int liaoStatus = PreferencesUtils.getInt(context, "liaoStatus");

        mLog.e("当前的聊天状态：liaoStatus=" + liaoStatus);

        if (liaoStatus != 2) {

            int temp = PreferencesUtils.getInt(context, "MishuMsg", 0);

            temp++;

            PreferencesUtils.putInt(context, "MishuMsg", temp);
            Log.e("test", "发送后的数量：MishuMsg=" + temp);

        }

        //刷新聊天界面

        if (mfindFaceLister != null) {

            mfindFaceLister.onSuccess("","");

        }

        // 广播通知
        Intent intent = new Intent();
        intent.setAction("action.refreshFriend");
        context.sendBroadcast(intent);


    }


    /**
     * 获取推送消息中机器人的名字
     * 权重：真心话>关注>单独打招呼
     */
    public static List<ZhenxinhuaFriend> zhenxinhuaFriends;
    public static List<GUANZHU> getguanzhuFriends;
    public static List<getChatFriendsResponse> chatFriends;

    public static void getJName(final Context context, final CallbackLister mLister) {
        zhenxinhuaFriends = new ArrayList<ZhenxinhuaFriend>();
        getguanzhuFriends = new ArrayList<GUANZHU>();
        chatFriends = new ArrayList<getChatFriendsResponse>();
        final String cid = PreferencesUtils.getString(context, "cid");

        zhenxinhuaFriends = zhenxinhuaFriendDao.query(context);
//        Log.e("test", "获取推送消息中机器人的名字：真心话好友数=" + zhenxinhuaFriends.size());

        if (zhenxinhuaFriends != null) {
            Log.e("test", "获取推送消息中机器人的名字：真心话好友数非空：获取name=");
            int temp=new Random().nextInt(zhenxinhuaFriends.size());
            mLister.onSuccess(zhenxinhuaFriends.get(temp).getJname(),zhenxinhuaFriends.get(temp).getJid());

        } else {
            Log.e("test", "获取推送消息中机器人的名字：真心话好友数为空：准备获取关注");
            final RequestParams params = new RequestParams();
            params.addBodyParameter("cid", cid);

            final HttpUtils http = new HttpUtils();
            http.send(HttpRequest.HttpMethod.POST,
                    MyAPI.getguanzhulist,
                    params,
                    new RequestCallBack<String>() {
                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            String result = XHttpUtils.Unicode2Chinese(responseInfo.result);
                            Log.e("test", "关注响应：" + result);
                            if (result.endsWith("null")) {
                                Log.e("test", "获取推送消息中机器人的名字：关注好友数玩为空：准备获取招呼");
                                http.send(HttpRequest.HttpMethod.POST,
                                        MyAPI.chatfriends,
                                        params,
                                        new RequestCallBack<String>() {
                                            @Override
                                            public void onSuccess(ResponseInfo<String> responseInfo) {
                                                String result = XHttpUtils.Unicode2Chinese(responseInfo.result);
                                                Log.e("test", "招呼响应：" + result);
                                                chatFriends = new Gson().fromJson(result, new TypeToken<ArrayList<getChatFriendsResponse>>() {
                                                }.getType());
                                                Log.e("test", "获取推送消息中机器人的名字：招呼好友数非空：获取name=");
                                                int temp=new Random().nextInt(chatFriends.size());
                                                mLister.onSuccess(chatFriends.get(temp).getName(),chatFriends.get(temp).getId());

//                                                if (result.endsWith("null")) {
//                                                    Log.e("test", "获取推送消息中机器人的名字：招呼好友数玩为空：");
//                                                } else {
//                                                    getguanzhuFriends = new Gson().fromJson(result, new TypeToken<ArrayList<GUANZHU>>() {
//                                                    }.getType());
//                                                    Log.e("test", "获取推送消息中机器人的名字：关注好友数非空：获取name=");
//
//                                                }
                                            }

                                            @Override
                                            public void onFailure(HttpException error, String msg) {
                                            }
                                        }
                                );



                            } else {
                                getguanzhuFriends = new Gson().fromJson(result, new TypeToken<ArrayList<GUANZHU>>() {
                                }.getType());
                                Log.e("test", "获取推送消息中机器人的名字：关注好友数非空：获取name=");
                                int temp=new Random().nextInt(getguanzhuFriends.size());
                                mLister.onSuccess(getguanzhuFriends.get(temp).getName(),getguanzhuFriends.get(temp).getId());

                            }
                        }

                        @Override
                        public void onFailure(HttpException error, String msg) {
                        }
                    }
            );

        }

    }


    /**
     * 接受缘分小秘书的消息
     * 2.封装消息
     * 4.添加消息到本地
     */
    public static void sendServiceMsg(final Context context, String msg, CallbackLister mfindFaceLister) {
        initSoudPool(context);

        //2.封装消息

        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

        ServiceMsg bean = new ServiceMsg();

        bean.setMsg(msg);
        bean.setType(ServiceMsg.RECEIVER);
        bean.setTime(time);


        mLog.e("接受缘分小秘书的消息:bean=" + new Gson().toJson(bean));


        //3.添加消息到本地

        ServiceMsgDao.add(context, bean);


        //消息提醒
//
//        mLogUtil.e("接受缘分小秘书的消息:播放音效");
//
//        soundPool.play(aid, 1, 1, 0, 0, 1);


        //5.配置数字相关

        //判断当前的聊天状态

//        int liaoStatus = PreferencesUtils.getInt(context, "liaoStatus");
//
//        mLogUtil.e("当前的聊天状态：liaoStatus=" + liaoStatus);
//
//        if (liaoStatus != 2) {
//
//            int temp = PreferencesUtils.getInt(context,  "MishuMsg", 0);
//
//            temp++;
//
//            PreferencesUtils.putInt(context, "MishuMsg", temp);
//
//        }

        //刷新聊天界面

        if (mfindFaceLister != null) {

            mfindFaceLister.onSuccess("","");

        }


    }


//自定义一个回调方法

    /**
     * 定义一个接口
     */
    public interface CallbackLister {
        public void onSuccess(String jname,String jid);

    }

    /**
     * 初始化接口变量
     */
    private static CallbackLister mfindFaceLister = null;

    /**
     * 自定义控件的自定义事件
     */
    public static void setfindFaceLister(CallbackLister mfindFaceLister2) {
        mfindFaceLister = mfindFaceLister2;
    }
}
